﻿//======================================================================
//
//        Copyright (C) 2016  
//        All rights reserved
//
//        filename : PutOutSendCard
//        description : 半成品/成品卡发放

//        created by 任卫华 at  20160113
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;

namespace CardLogistics.Web.MainPage.PutOutCard.PutOutSendCard
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.PutOutCard.PutOutSendCardData DAL = new DAL.PutOutCard.PutOutSendCardData();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            {
                BindDLL(ddlsupName, "basic_suppliers", "suppliersname", "suppliersid", " and STATUS=0 and ISDELETE=0");  //供应商
                BindDLL(ddlCardType, "basic_cardtype", "CARDTYPENAME", "ID", " and ISDELETE=0  ");//绑定卡类型
                BindDLL(ddlLEADINGUNIT, "BASIC_SALESINFO", "SALESNAME", "ID", " and ISDELETE=0  ");//销售机构/网点
                //状态 0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                BindDLL(ddlIsSubmit);
                BindRpt(LoadStrWhere);
            }
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = this.hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = DAL.Delete(ids[i]);
                    if (resStr.Split(',')[0] == "ok")
                        resCount++;
                }
                BindRpt("");
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(string strWhere)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" 1=1 ");
            if (txt_SENDNO.Text.Trim().Length > 0)
                sbStr.Append(" and SENDNO like '%" + txt_SENDNO.Text.Trim().ToUpper() + "%' ");//发放单号
            if (txt_PUTOUTCARDNO.Text.Trim().Length > 0)
                sbStr.Append(" and PUTOUTCARDNO like '%" + txt_PUTOUTCARDNO.Text.Trim().ToUpper() + "%' ");//出库单号
            if (txt_StartLEADINGDATE.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(LEADINGDATE,'yyyy-MM-dd')>='" + txt_StartLEADINGDATE.Text.Trim() + "'");//领用开始日期
            if (txt_EndLEADINGDATE.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(LEADINGDATE,'yyyy-MM-dd')<='" + txt_EndLEADINGDATE.Text.Trim() + "'");//领用结束日期
            if (this.ddlsupName.SelectedItem != null && ddlsupName.SelectedIndex != 0)
                sbStr.Append(" and SUPPLIERSNAME='" + ddlsupName.SelectedItem.Text.ToString() + "'");//供应商
            if (this.ddlCardType.SelectedItem != null && ddlCardType.SelectedIndex != 0)
                sbStr.Append(" and cardtypeName='" + ddlCardType.SelectedItem.Text.ToString() + "'");//卡类型
            if (this.ddlLEADINGUNIT.SelectedItem != null && ddlLEADINGUNIT.SelectedIndex != 0)
                sbStr.Append(" and SalesName='" + ddlLEADINGUNIT.SelectedItem.Text.ToString() + "'");//销售机构/网点
            //审核状态
            if (ddlIsSubmit.SelectedValue != "-1")
            {
                sbStr.Append(" and IsSubmit="+ddlIsSubmit.SelectedValue);
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                sbStr.Append(strWhere);
            }

            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_PutOutSendCard", "ID", " issubmit,leadingdate desc, sendno desc", WebPageSize, WebPageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);

            #region 导出数据
            //导出语句
            string strExpSql = "select SENDNO 发放单号,SendCardTotal 发放数量,PUTOUTCARDNO 出库单号,SUPPLIERSNAME 供应商,cardtypeName 卡类型,LEADINGUNIT 领用单位,LEADINGPERSON 领用人,LEADINGDATE 领用日期,PUTINEDTOTAL 需出库数量,REMARKS 备注 from v_PutOutSendCard where " + sbStr.ToString() + " order by SENDNO desc";
            if (!Page.IsClientScriptBlockRegistered("regExportSql"))
                Page.RegisterClientScriptBlock("regExportSql", "<script language=\"Javascript\">var qsql=\"" + Server.UrlEncode(strExpSql) + "\";</script>");
            #endregion
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt("");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            WebPageIndex = 1;
            BindRpt("");
        }
    }
}